<template>
<div class="app-container">
    <el-form ref="form" :model="User" label-width="120px">
        <el-form-item label="用户名">
            <el-input v-model="User.username" disabled />
        </el-form-item>
        <el-form-item label="手机号码">
            <el-input v-model="User.telephone" />
        </el-form-item>
        <el-form-item label="E-mail">
            <el-input v-model="User.email" />
        </el-form-item>
        <el-form-item label="真实姓名">
            <el-input v-model="User.realname" />
        </el-form-item>
        <el-form-item label="身份证号码">
            <el-input v-model="User.idnumber" />
        </el-form-item>
        <el-form-item label="账户余额">
            <el-input v-model="User.balance" />
        </el-form-item>
        <el-form-item label="收货地址">
            <el-select v-model="User.address">
                <el-option v-for="address in AddressList" :key="address.id">{{address.address}}</el-option>
            </el-select>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="edit">Edit</el-button>
        </el-form-item>
    </el-form>
</div>
</template>

<script>
export default {
    data: function () {
        return {
            User: {},
            AddressList: null
        };
    },
    mounted() {
        //加载时回填用户信息
        this.getInfo();
    },
    methods: {
        //获取用户信息
        getInfo() {
            var paramId = this.$route.params.id;
            console.log(paramId);
            this.axios({
                url: "http://localhost:8888/user/user/user/getUserById",
                method: "get",
                params: {
                    id: paramId
                },
            }).then((res) => {
                this.User = res.data.data.user;
            });
        },

        //拉取地址信息
        getAddress() {
            var paramId = this.$route.params.id;
            this.axios({
                url: "http://localhost:8888/user/user/address/getAddressById",
                method: "get",
                params: {
                    id: paramId
                },
            }).then((res) => {
                this.AddressList = res.data.data.address;
            });
        },
        //修改请求
        edit() {
            var paramId = this.$route.params.id;
            this.axios({
                url: "http://localhost:8888/user/user/user/update",
                method: "put",
                data: JSON.stringify(this.User),
                headers: {
                    "Content-Type": "application/json",
                },
            }).then((res) => {
                console.log(res.data.code)
                if (res.data.code == 200) {
                    //跳转页面
                    this.$message({
                        message: '修改成功',
                        type: 'success'
                    });
                    this.$router.push("/user/userList")
                }
            });
        },
    },
};
</script>

<style>
</style>
